var app = getApp();
import { api, forms, util, dialog } from '../../../../js/util'
import {
  router
} from '../../../../js/router';
import { retreat } from "../../../../js/modules/order.js"
import { address } from "../../../../js/modules/store.js"
import {
  wholesaleSettings
} from "../../../../js/modules/gloab";
import BasePage from '../../../../js/common/base-page'
import {list} from '../../../../js/modules/mall';
BasePage({
  pageKey:"retreatDetail",

  /**
   * 页面的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    retreat: {},
    reItem: [],
    retLogistics: [],
    retreatLog: [],
    logistics:{
      arr:[],
      list:[],
      index:0,
      oddNumber:""
    },
    defaultAdr:{},
    history: true,
    oddnumber:"",
    quantityDigits: 2,
    priceDigits: 2,//价格小数位数
    headerBg:"",
    Currency:'',//货币符号
    ReturnWithoutBuyers:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var oddNumber = decodeURIComponent(options.oddNumber);
    this.data.oddnumber=oddNumber;
    this.getDetail(oddNumber);
    this.getWholesaleConfig();
  },
  /**加载订单信息 */
  getDetail: function (oddNumber) {
    var that = this;
    retreat.getDetail(oddNumber, function (retreat, reItem, retLogistics, retreatLog) {
      that.setData({
        retreat: retreat,
        reItem: reItem,
        retLogistics: retLogistics,
        retreatLog: retreatLog
      });
      //发货时加载物流公司
      if (retreat.Status == 10){
        that.getLogisticsList(that);
      }
      //发货时，换货返修才选择地址
      if (retreat.Status == 10 && (retreat.ReType == 2 || retreat.ReType==3)) {
        that.getDefaultAddress(that);
      }
    });
  },
  /**
   * 获得物流公司
   */
  getLogisticsList:(that)=>{
    retreat.getLogisticsList(function (data) {
      for(var i=0;i<data.length;i++){
        that.data.logistics.arr.push(data[i].Name);
      }
      that.setData({
        "logistics.list": data,
        "logistics.arr": that.data.logistics.arr
      });
    });
  },
  /**
   * 获得默认地址
   */
  getDefaultAddress: (that) => {
    address.getDefaultAddress(function(ads) {
      if (ads!=undefined&&Object.keys(ads).length>0) {
        that.setData({
          "defaultAdr": ads
        });
      }
    });
  },
  /**加载配置项参数 */
  getWholesaleConfig() {
    var that = this;
    var config = wholesaleSettings.getConfig();
    var fkid = app.dns.proprietorId;
    var fkflag = app.dns.proprietor;
    list.getWholesaleConfigs(
      fkid,
      fkflag,
      (content)=> {
        that.setData({
          ReturnWithoutBuyers:content.ReturnWithoutBuyers
        });
      })
    that.setData({
      quantityDigits: config.quantityDigits,
      priceDigits: config.priceDigits
    });
  },
  /**
   * 取消申请
   */
  onCancelApplayEvent:function(e){
    var that=this;
    retreat.cancelApplay(this.data.retreat.Id,function(){
      that.getDetail(that.data.oddnumber)
    });
  },
  /**
   * 确认收货
   */
  onConfirmReceiveEvent:function(e){
    var that=this;
    retreat.onConfirmReceive(this.data.retreat.Id, function (res) {
      that.getDetail(that.data.oddnumber)
    });
  },
  /**
   * 确认发货
   */
  onSendEvent:function(e){
    var id=0;
    var that=this;
    var retreatId=this.data.retreat.Id;
    for (var i = 0; i < this.data.retLogistics.length;i++){
      if (this.data.retLogistics[i].IsTaken){
        id = this.data.retLogistics[i].Id;
        break;
      }
    }
    //bug:13160    要求将快递公司、快递单号均改为非必填 特此注释
    // if (this.data.logistics.oddNumber == undefined || this.data.logistics.oddNumber.length==0){
    //   util.tips("请输入快递单号");
    //   return;
    // }
    // var oddItem = {
    //   value: util.trim(this.data.logistics.oddNumber),
    //   validate: "isnull|oddNumber",
    //   error: "请输入正确的快递单号"
    // }
    // if (!forms.validate(oddItem)) return false;
    var address = this.data.defaultAdr;
    var data={
      RetreatId: retreatId,
      Id:id,
      LogisticsName: this.data.logistics.list[this.data.logistics.index].Name,
      OddNumber: this.data.logistics.oddNumber,
      TakeAddress: address.Province + address.City + address.Area + address.Address + "," + address.Name + "," + address.Mobile,
      Retreat: retreat.getSendRetreatXml(retreatId, this.data.reItem)
    }
    retreat.sendRetreat(data, function (res) {
      that.hideModal(e);
      that.getDetail(that.data.oddnumber)
    });
  },
  /**
   * 物流公司选择事件
   */
  onLogisticsChangeEvent:function(e){
    // console.log(e.detail.value);
    this.setData({
      "logistics.index": e.detail.value
    })
  },
  /**
 * 表单值绑定
 */
  bindFormValue: function (e) {

    this.data.logistics[e.target.id] = e.detail.value;

  },
  /**
   * 重新选择收货地址
   */
  onSelectAddressEvent:function(e){
    router.navigate({
      path: "addressList",
      params: {
        id: this.data.defaultAdr.Id
      }
    }); 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      headerBg:api.titleColor||"#e56b14",
      Currency:api.Currency || '￥'
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
  * 页面相关事件处理函数--监听用户下拉动作
  */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh();
    this.getDetail(this.data.oddNumber);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
 * 展开遮罩层
 */
  showModal: function (e) {
    var id = e.currentTarget.dataset.orderid;
    this.data.orderId = id;
    dialog.showModal(this, 'showModalStatus')
  },
  /**
   * 隐藏遮罩层
   */
  hideModal: function () {
    dialog.hideModal(this, 'showModalStatus')
  },
  /**
   * 是否显示历史
   */
  showHistory: function () {
    this.setData({
      history: !this.data.history
    })
  },
  imageTab:function(e){
    let img = e.currentTarget.dataset.src;
    wx.previewImage({
      current: img, // 当前显示图片的http链接
      urls: [img] // 需要预览的图片http链接列表
    })
  },
  copy (e) {
    let item = e.currentTarget.dataset.item || {}
    wx.setClipboardData({
      data: item.TakeAddress,
      fail (err) {
        console.log(err)
      }
    })
  }
})